﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Project.API.Application.Commands;
using Project.API.Application.Queries;
using Project.Domain.AggregateModels;
using Project.Domain.SeedWork;

namespace Project.API.Controllers
{
    [Route("api/[controller]/[action]")]
    public class ProjectsController
        : BaseController
    {
        private readonly IMediator _mediator;
        private readonly IProjectQueries _projectQueries;
        public ProjectsController(IMediator mediator,
            IProjectQueries projectQueries)
        {
            _mediator = mediator;
            _projectQueries = projectQueries;
        }
        [HttpPost]
        public async Task<IActionResult> CreateProjectAsync(Domain.AggregateModels.Project project)
        {
            if (project == null)
            {
                throw new ArgumentNullException("project is null");
            }
            project.UserId = UserIdentity.Id;
            var cmd = new CreateProjectCmd()
            {
                Project = project
            };
            await _mediator.Send(cmd);
            return Ok();
        }
        [HttpGet]
        public async Task<IActionResult> GetProjectsByUserId()
        {
            var belongs = Enumeration.GetAll<BelongTo>();
            var val = Enumeration.FromDisplayName<BelongTo>("First");
            var name = Enumeration.FromValue<BelongTo>(1);
            var list = await _projectQueries.GetProjectsByUserIdAsync(UserIdentity.Id);
            return Json(list);
        }
    }
}
